package com.maxflow.hadoop;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;

public class ReducerClass extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
	public static int max = 0;
	public static String text = "";
	
	// reduce method accepts the Key Value pairs from mappers, do the
	// aggregation based on keys and produce the final out put
	public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
		int sum = 0;
		/*
		 * iterates through all the values available with a key and add them
		 * together and give the final result as the key and sum of its values
		 */
		while (values.hasNext()) {
			sum += values.next().get();
		}

		if (sum > max) {
			max = sum;
			text = key.toString();
		}
		
		output.collect(key, new IntWritable(sum));
	}
}
